package online.flowerinsnow.minecraft.bukkit.floweress

import online.flowerinsnow.minecraft.bukkit.floweress.command.FlowerAIRadarAntiCheatCommand
import online.flowerinsnow.minecraft.bukkit.floweress.command.FlyCommand
import online.flowerinsnow.minecraft.bukkit.floweress.core.FlowerEssCore
import online.flowerinsnow.minecraft.bukkit.floweress.listener.JoinLeftListener
import online.flowerinsnow.minecraft.bukkit.floweress.message.MessageConfig
import org.bukkit.command.TabExecutor
import org.bukkit.event.Listener
import org.bukkit.plugin.java.JavaPlugin

class FlowerEss : JavaPlugin() {
    override fun onEnable() {
        instance = this

        MessageConfig.saveDefaultConfig()
        MessageConfig.reloadConfig()

        registerCommand("fly", FlyCommand())
        registerCommand("flowerairadaranticheat", FlowerAIRadarAntiCheatCommand())

        server.pluginManager.registerEvents(JoinLeftListener(), this)

        FlowerEssCore.init()
    }

    override fun onDisable() {

    }

    fun registerListeners(vararg listeners : Listener) {
        listeners.forEach { listener ->
            server.pluginManager.registerEvents(listener, this)
        }
    }

    fun registerCommand(name : String, executor : TabExecutor) {
        val cmd = getCommand(name)
        if (cmd == null) {
            logger.warning("Command \"$name\" not found.")
        } else {
            cmd.setExecutor(executor)
            cmd.tabCompleter = executor
        }
    }

    companion object {
        lateinit var instance : FlowerEss
        private set
    }
}